package com.example.a053checkbox_switch_radiobutton_imageview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class CheckBoxActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private TextView mTextView;
    private CheckBox mCheckBox;
    private CheckBox mCheckBox2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_box);
        initView();
    }

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        CheckBox checkedCheckBox = (CheckBox) compoundButton;
        String message;
        if (isChecked) {
            message = String.format("控件:%s; ID:%d; 被选中", checkedCheckBox.getText(), checkedCheckBox.getId());
        } else {
            message = String.format("控件:%s; ID:%d; 被取消", checkedCheckBox.getText(), checkedCheckBox.getId());
        }
        mTextView.setText(message);
    }

    private void initView() {
        mTextView = findViewById(R.id.textView);
        mCheckBox = findViewById(R.id.checkBox);
        mCheckBox.setOnCheckedChangeListener(this);
        mCheckBox2 = findViewById(R.id.checkBox2);
        mCheckBox2.setOnCheckedChangeListener(this);
    }


}